﻿using System;
using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg
{
    class IndexAndContextExpectingProduction : FakeParsingProduction
    {
        private ParseResult result;
        private int expectedIndex;
        private readonly ParseContext expectedParseContext;

        public IndexAndContextExpectingProduction(ParseResult result, int expectedIndex, ParseContext expectedParseContext)
        {
            this.result = result;
            this.expectedIndex = expectedIndex;
            this.expectedParseContext = expectedParseContext;
        }

        public override Yield Parse(char[] source, int index, ParseContext context, string sourceString, ParseEnvironment parseEnvironment, Func<ParseResult, Yield> continuation)
        {
            Assert.That(index, Is.EqualTo(expectedIndex));
            if (expectedParseContext != null)
            {
                Assert.That(context, Is.SameAs(expectedParseContext));
            }
            return () => continuation(result);
        }
    }
}